package model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

public class Brain {
	private Map<String, User> users;
	private List<Movie> movies;
	private int ratingId;

	public Brain() {
		ratingId = 1;
		
		users = new HashMap<String, User>();
		movies = new ArrayList<Movie>();

		fillBrain();
		
		User k = new User("Reimert", "Kevin", "niek", "test");
		User g = new User("Gottemaker", "Gerralt", "Gerralt", "wachtwoord");
		
		addUser(k);
		addUser(g);
		
		addRating(k, movies.get(0), 2.5);
		addRating(k, movies.get(1), 4.5);
		addRating(g, movies.get(0), 3.5);
	}

	public List<User> getUsers() {
		List<User> toReturn = new ArrayList<User>();
		for(User user : users.values()){
			toReturn.add(user);
		}
		
		return toReturn;
	}
	
	public User getUser(String nickname) {
		for(User user : users.values()){
			if(user.getNickname().equals(nickname)) {
				return user;
			}
		}
		return null;
	}

	public List<Movie> getMovies() {
		return movies;
	}

	public Movie getMovie(String imdbttnumber) {
		for(Movie m : movies) {
			if(m.getImdbtt().equals(imdbttnumber)) {
				return m;
			}
		}
		return null;
	}
	
	public String addUser(User user){
		if(!nicknameExists(user.getNickname())){
			String token = generateToken();
			System.out.println("created token: " + token);
			users.put(token, user);
			return token;
		}
		return null;
	}
	
	public User getUserByToken(String token) {
		return users.get(token);
	}
	
	public String authenticate(String username, String password){
		if(nicknameExists(username)){
			for(Entry<String, User> keyset : users.entrySet()){
				if(keyset.getValue().getNickname().equalsIgnoreCase(username) && keyset.getValue().getPassword().equalsIgnoreCase(password)){
					return keyset.getKey();
				}
			}
		}
		return null;
	}
	
	public int addRating(User user, Movie movie, double rating) {
		int ratingId = getRatingId();
		
		user.addRating(new Rating(ratingId, rating, movie));

		return ratingId;
	}

	public boolean movieHasRating(Movie movie, User user) {
		return (user.getRatingByMovie(movie) != null);
	}

	public List<Rating> getRatingsByMovie(Movie movie) {
		List<Rating> toReturn = new ArrayList<Rating>();
		for(User u : users.values()) {
			Rating r = u.getRatingByMovie(movie);
			if(r != null) {
				toReturn.add(r);
			}
		}
		return toReturn;
	}
	
	private boolean nicknameExists(String nickname){
		for(User u : users.values()){
			if(u.getNickname().equalsIgnoreCase(nickname)){
				return true;
			}			
		}
		return false;
	}
	
	private String generateToken() {
		String uuid = UUID.randomUUID().toString();
		while(users.containsKey(uuid)) {
			uuid = UUID.randomUUID().toString();
		}
		return uuid;
	}
	
	private int getRatingId() {
		return ratingId++;
	}
	
	private void fillBrain() {
		Calendar date = Calendar.getInstance();
		Movie movie = null;
		
		date.set(1995, 3, 2);
		movie = new Movie(1, "0111161", "The Shawshank Redemption", date, 142, "Frank Darabont", "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.");
		movies.add(movie);
		
		date.set(1973, 1, 18);
		movie = new Movie(2, "0068646", "The Godfather", date, 175, "Francis Ford Coppola", "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.");
		movies.add(movie);
		
		date.set(1975, 7, 17);
		movie = new Movie(3, "0071562", "The Godfather: Part II", date, 200, "Francis Ford Coppola", "The early life and career of Vito Corleone in 1920s New York is portrayed while his son, Michael, expands and tightens his grip on his crime syndicate stretching from Lake Tahoe, Nevada to pre-revolution 1958 Cuba.");
		movies.add(movie);
		
		date.set(2008, 7, 24);
		movie = new Movie(4, "0468569", "The Dark Knight", date, 152, "Christopher Nolan", "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.");
		movies.add(movie);
		
		date.set(1994, 12, 1);
		movie = new Movie(5, "0110912", "Pulp Fiction", date, 154, "Quentin Tarantino", "The lives of two mob hit men, a boxer, a gangster's wife, and a pair of diner bandits intertwine in four tales of violence and redemption.");
		movies.add(movie);
	}
}
